package com.ruoyi.web.controller.indexAdvertising;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.indexAdvertising.domain.IndexAdvertising;
import com.ruoyi.indexAdvertising.service.IIndexAdvertisingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 启动页广告Controller
 *
 * @author jiuyou
 * @date 2021-10-18
 */
@RestController
@RequestMapping("/system/index/advertising")
public class IndexAdvertisingController extends BaseController
{
    @Autowired
    private IIndexAdvertisingService indexAdvertisingService;

    /**
     * 查询启动页广告列表
     */
    @PreAuthorize("@ss.hasPermi('system:advertising:list')")
    @GetMapping("/list")
    public TableDataInfo list(IndexAdvertising indexAdvertising)
    {
        startPage();
        List<IndexAdvertising> list = indexAdvertisingService.selectIndexAdvertisingList(indexAdvertising);
        return getDataTable(list);
    }

    /**
     * 停用
     */
    @PreAuthorize("@ss.hasPermi('system:advertising:edit')")
    @GetMapping("/stop/{id}")
    public AjaxResult stop(@PathVariable Long id)
    {
        return toAjax(indexAdvertisingService.stop(id));
    }

    /**
     * 查询启动页广告列表
     */
    @PreAuthorize("@ss.hasPermi('system:advertising:edit')")
    @GetMapping("/start/{id}")
    public AjaxResult start(@PathVariable Long id)
    {
        IndexAdvertising indexAdvertising = new IndexAdvertising();
        indexAdvertising.setShowState("Y");
        List<IndexAdvertising> list = indexAdvertisingService.selectIndexAdvertisingList(indexAdvertising);
        if (list.size() > 0) {
            throw new CustomException("当前已有启用数据,请先停用该数据");
        }
        return toAjax(indexAdvertisingService.start(id));
    }

    /**
     * 导出启动页广告列表
     */
    @PreAuthorize("@ss.hasPermi('system:advertising:export')")
    @Log(title = "启动页广告", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(IndexAdvertising indexAdvertising)
    {
        List<IndexAdvertising> list = indexAdvertisingService.selectIndexAdvertisingList(indexAdvertising);
        ExcelUtil<IndexAdvertising> util = new ExcelUtil<IndexAdvertising>(IndexAdvertising.class);
        return util.exportExcel(list, "advertising");
    }

    /**
     * 获取启动页广告详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:advertising:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(indexAdvertisingService.selectIndexAdvertisingById(id));
    }

    /**
     * 新增启动页广告
     */
    @PreAuthorize("@ss.hasPermi('system:advertising:add')")
    @Log(title = "启动页广告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IndexAdvertising indexAdvertising)
    {

        IndexAdvertising to = new IndexAdvertising();
        to.setShowState("Y");
        List<IndexAdvertising> list = indexAdvertisingService.selectIndexAdvertisingList(to);
        if (list.size() > 0) {
            throw new CustomException("当前已有启用数据,请先停用该数据");
        }
        return toAjax(indexAdvertisingService.insertIndexAdvertising(indexAdvertising));
    }

    /**
     * 修改启动页广告
     */
    @PreAuthorize("@ss.hasPermi('system:advertising:edit')")
    @Log(title = "启动页广告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IndexAdvertising indexAdvertising)
    {
        return toAjax(indexAdvertisingService.updateIndexAdvertising(indexAdvertising));
    }

    /**
     * 删除启动页广告
     */
    @PreAuthorize("@ss.hasPermi('system:advertising:remove')")
    @Log(title = "启动页广告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(indexAdvertisingService.deleteIndexAdvertisingByIds(ids));
    }
}
